﻿using ProtoBuf.Message;
using System;
using System.Collections.Generic;
using Dict.DataModel;
using Dict.Blo;
using Dict.Config;

namespace UI
{
    public class UIEquipment
    {
        public TableEquip equipData;
        public TableEquipRefine equipRefineData;
        public string guid;
        public string equipId;
        public string lastHeroId;
        public int level;
        public int refineLv;
        public int refineExp;
        public int star;
        public int starExp;
        public int coinCost;  //强化过程中花费的金币
        public int[] holes;   //装备可镶嵌孔
        public List<string> gems = new List<string>();    //镶嵌宝石状况
        public int forgeLv;
        List<TableEquipForgeProp> equipForgeData = new List<TableEquipForgeProp>();
        public string name
        {
            get
            {
                return TableTextBlo.GetText("name_" + equipId);
            }
        }
        public string iconPath
        {
            get
            {
                string heroId = "";
                if (owner != null)
                    heroId = owner.hero.heroId;
                else
                    heroId = lastHeroId;

                if (heroId != "")
                {
                    List<TableWeaponRes> weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroId, equipId);
                    if (weapons.Count > 0)
                    {
                        return "EquipIcons/" + weapons[0].iconRes;
                    }
                }
                if (equipData != null)
                {
                    return "EquipIcons/" + equipData.icon;
                }
                else
                {
                    return null;
                }
            }
        }

        public string texturePath
        {
            get
            {
                string heroId = "";
                if (owner != null)
                    heroId = owner.hero.heroId;
                else
                    heroId = lastHeroId;

                if (heroId != "")
                {
                    List<TableWeaponRes> weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroId, equipId);
                    if (weapons.Count > 0)
                    {
                        return "Equip2d/" + weapons[0].pngRes;
                    }
                }
                if (equipData != null)
                {
                    return "Equip2d/" + equipData.icon.Replace("icon", "png");
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 总属性
        /// </summary>
        private BaseAttributeData currentAttribute;
        /// <summary>
        /// 等级属性
        /// </summary>
        private BaseAttributeData currentLevAttribute;
        /// <summary>
        /// 精炼属性
        /// </summary>
        private BaseAttributeData currentRefineAttribute;
        /// <summary>
        /// 星级属性
        /// </summary>
        //private BaseAttributeData currentStarAttribute;

        /// <summary>
        /// 宝石属性
        /// </summary>
        private BaseAttributeData currentGemsAttribute;

        public BaseAttributeData CurrentGemsAttribute
        {
            get
            {
                return currentGemsAttribute;
            }
        }

        /// <summary>
        /// 锻造属性
        /// </summary>
        private BaseAttributeData currentForgeAttribute;
        #region 辅助
        public UIHeroFormation owner;
        public UIInstPlayerData simplePlayer;
        #endregion

        #region 初始化
        public UIEquipment(string id, UIInstPlayerData simplePlayer)
        {
            this.simplePlayer = simplePlayer;
            this.equipData = DictEquipBlo.GetTableEquip(id);
            if (equipData == null)
            {
                throw new System.NullReferenceException("equipid:" + id);
            }

            this.equipRefineData = DictEquipRefineBlo.GetTableEquipRefine(this.equipData.type, this.equipData.quality);
            this.currentAttribute = new BaseAttributeData(0);
            this.currentLevAttribute = new BaseAttributeData(0);
            this.currentRefineAttribute = new BaseAttributeData(0);
            //this.currentStarAttribute = new BaseAttributeData(0);
            this.currentGemsAttribute = new BaseAttributeData(0);
            this.currentForgeAttribute = new BaseAttributeData(0);
            InitHoles(equipData);
        }
        public UIEquipment(string id)
        {
            this.equipData = DictEquipBlo.GetTableEquip(id);
            this.equipId = equipData.id;
            this.level = 1;
            this.starExp = 0;
            this.refineLv = 0;
            //this.currentExp = 0;
            this.equipRefineData = DictEquipRefineBlo.GetTableEquipRefine(equipData.type, equipData.quality);
            this.currentRefineAttribute = new BaseAttributeData(0);
            this.currentLevAttribute = new BaseAttributeData(0);
            this.currentAttribute = new BaseAttributeData(0);
            this.currentGemsAttribute = new BaseAttributeData(0);
            this.currentForgeAttribute = new BaseAttributeData(0);
            InitHoles(equipData);
            this.forgeLv = 0;
            this.equipForgeData = Dict.DictDataManager.Instance.tableEquipForgePropDao.GetByOneIndex(id);

            UpdateLevAttribute();
            ResetAttribute();

        }
        public UIEquipment(ProtoBuf.Message.EquipmentSimpleInfo data, string heroid)
        {
            this.equipData = DictEquipBlo.GetTableEquip(data.equipId);
            this.equipForgeData = Dict.DictDataManager.Instance.tableEquipForgePropDao.GetByOneIndex(data.equipId);
            equipId = data.equipId;
            level = data.level;
            star = data.star;
            refineLv = data.refineLv;
            lastHeroId = heroid;
            gems = data.gems;
            InitHoles(equipData);
            forgeLv = data.forgeLevel;
        }

        //镶嵌宝石孔信息
        void InitHoles(TableEquip equipData)
        {
            holes = equipData.hole_array;
        }

        public void InitData(Equipment data)
        {
            guid = data.guid;
            equipId = data.equipId;
            lastHeroId = data.lastHeroId;
            level = data.level;
            refineLv = data.refineLv;
            refineExp = data.refineExp;
            star = data.star;
            starExp = data.starExp;
            coinCost = data.coinPrice;
            gems = data.gems;
            forgeLv = data.forgeLevel;
            UpdateLevAttribute();
            UpdateRefineLevAttribute();
            UpdateGemsAttribute(gems);
            UpdateForgeLevAttribute();
            ResetAttribute();
        }


        private void UpdateLevAttribute()
        {
            currentLevAttribute.SetProp(AttributeQueue.Attack, equipData.attBase + equipData.attGrow * (level - 1));
            currentLevAttribute.SetProp(AttributeQueue.Hp, equipData.hpBase + equipData.hpGrow * (level - 1));
            currentLevAttribute.SetProp(AttributeQueue.MagicDefend, equipData.mdefBase + equipData.mdefGrow * (level - 1));
            currentLevAttribute.SetProp(AttributeQueue.PhyDefend, equipData.pdefBase + equipData.pdefGrow * (level - 1));
        }

        private void UpdateRefineLevAttribute()
        {
            currentRefineAttribute.Reset();
            if (equipRefineData != null)
            {
                currentRefineAttribute.SetProp(AttributeQueue.Attack, equipRefineData.attGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.MagicDefend, equipRefineData.mdefGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.Hp, equipRefineData.hpGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.PhyDefend, equipRefineData.pdefGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.dodge, equipRefineData.perevadeGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.cri, equipRefineData.criGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.hit, equipRefineData.perhitGrow * refineLv);
                currentRefineAttribute.SetProp(AttributeQueue.antiCri, equipRefineData.anticriGrow * refineLv);
            }

            List<TableEquipSkill> tess = DictEquipSkillBlo.GetTableEquipSkills(equipData.id, refineLv);
            char[] sp = { ',' };
            for (int i = 0; i < tess.Count; ++i)
            {
                if (!string.IsNullOrEmpty(tess[i].bonus))
                {
                    string[] prop = tess[i].bonus.Split(sp, System.StringSplitOptions.RemoveEmptyEntries);
                    currentRefineAttribute.AddProp(prop[0], double.Parse(prop[1]));
                }
            }
        }

        /// <summary>
        /// 锻造属性
        /// </summary>
        private void UpdateForgeLevAttribute()
        {
            currentForgeAttribute.Reset();
            this.equipForgeData = Dict.DictDataManager.Instance.tableEquipForgePropDao.GetByOneIndex(equipId);
            if (equipForgeData.Count>0)
            {
                var str = equipForgeData[0].prop;
                string[] sArray = str.Split(',');
                switch (sArray[0])
                {
                    case "att":
                        double attNum = 0;
                        foreach (var aa in equipForgeData)
                        {
                            string[] sProp = aa.prop.Split(',');
                            if(aa.forgeLevel <= forgeLv)
                            {
                                attNum += double.Parse(sProp[1]);
                            } 
                        }
                        currentForgeAttribute.AddProp(AttributeQueue.Attack, attNum);
                        break;
                    case "mdef":
                        double mdefNum = 0;
                        foreach (var aa in equipForgeData)
                        {
                            string[] sProp = aa.prop.Split(',');
                            if(aa.forgeLevel <= forgeLv)
                            {
                                mdefNum += double.Parse(sProp[1]);
                            } 
                        }
                        currentForgeAttribute.AddProp(AttributeQueue.MagicDefend, mdefNum);
                        break;
                    case "hp":
                        double hpNum = 0;
                        foreach (var aa in equipForgeData)
                        {
                            string[] sProp = aa.prop.Split(',');
                            if(aa.forgeLevel <= forgeLv)
                            {
                                hpNum += double.Parse(sProp[1]);
                            } 
                        }
                        currentForgeAttribute.AddProp(AttributeQueue.Hp, hpNum);
                        break;
                    case "pdef":
                        double pdefNum = 0;
                        foreach (var aa in equipForgeData)
                        {
                            string[] sProp = aa.prop.Split(',');
                            if(aa.forgeLevel <= forgeLv)
                            {
                                pdefNum += double.Parse(sProp[1]);
                            } 
                        }
                        currentForgeAttribute.AddProp(AttributeQueue.PhyDefend, pdefNum);
                        break;
                }
            }
          
        }

        /// <summary>
        /// 宝石属性
        /// </summary>
        private void UpdateGemsAttribute(List<string> gemsID)
        {
            currentGemsAttribute.Reset();
            List<TableMineSkill> equipGemsData = new List<TableMineSkill>();
            for (int i = 0; i < gemsID.Count; i++)
            {
                if (!string.IsNullOrEmpty(gemsID[i]))
                    equipGemsData.Add(DictMineBlo.GetTableMineSkill(gemsID[i]));
            }
            for (int num = 0; num < equipGemsData.Count; num++)
            {
                string[] attributes = equipGemsData[num].Value.Split(';');
                for (int i = 0; i < attributes.Length; ++i)
                {
                    string[] prop = attributes[i].Split(',');
                    double value = double.Parse(prop[1]);
                    currentGemsAttribute.AddProp(prop[0], value);
                }
            }
        }

        #endregion

        #region 数据变更
        /// <summary>
        /// 升级
        /// </summary>
        /// <param name="newLev"></param>
        public void UpdateLev(int newLev)
        {
            level = newLev;
            UpdateLevAttribute();
            ResetAttribute();
        }

        /// <summary>
        /// 精炼
        /// </summary>
        /// <param name="newLev"></param>
        /// <param name="exp"></param>
        public void UpdateRefineLev(int newLev, int exp)
        {
            refineLv = newLev;
            refineExp = exp;
            UpdateRefineLevAttribute();
            ResetAttribute();
        }

        /// <summary>
        /// 升星
        /// </summary>
        /// <param name="newLev"></param>
        /// <param name="exp"></param>
        public void UpdateStarLev(int newLev, int exp)
        {
            star = newLev;
            starExp = exp;
            ResetAttribute();

        }
        /// <summary>
        /// 锻造
        /// </summary>
        /// <param name="newLev"></param>
        /// <param name="exp"></param>
        public void UpdateForgeLev(int newLev)
        {
            forgeLv = newLev;
            UpdateForgeLevAttribute();
            ResetAttribute();
        }

        #endregion

        #region Tool

        /// <summary>
        /// 装备是否培养过
        /// </summary>
        /// <returns></returns>
        public bool IsZeroEquipment()
        {
            if(level == 1 && refineLv == 0 && forgeLv == 0)
            {
                return true;
            }
            return false;
        }

        private void ResetAttribute()
        {
            currentAttribute.Reset();
            currentAttribute.AddProp(currentLevAttribute);
            currentAttribute.AddProp(currentRefineAttribute);
            currentAttribute.AddProp(currentForgeAttribute);
        }

        /// <summary>
        /// 获取攻击 防御 等属性
        /// </summary>
        /// <param name="propId"></param>
        /// <returns></returns>
        public double GetProp(AttributeQueue propId)
        {
            return currentAttribute.GetProp(propId);
        }

        public double GerPorpAddPercent(string propId)
        {
            return 0;
        }
        /// <summary>
        /// 总属性
        /// </summary>
        public BaseAttributeData CurrentAttribute
        {
            get
            {
                return currentAttribute;
            }
        }
        public BaseAttributeData CurrentLevAttribute
        {
            get { return currentLevAttribute; }
        }


        public BaseAttributeData CurrentPercentAttribute
        {
            get
            {
                return BaseAttributeData.zero;
            }
        }

        /// <summary>
        /// 获取某一属性在强化等级X时的数值
        /// </summary>
        /// <param name="propId"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public float GetEquipPropInLevel(AttributeQueue propId, int level)
        {
            float prop = 0;

            switch (propId)
            {
                case AttributeQueue.Attack:
                    prop = (float)(currentAttribute.GetProp(propId) + (level - this.level) * this.equipData.attGrow);
                    break;
                case AttributeQueue.Hp:
                    prop = (float)(currentAttribute.GetProp(propId) + (level - this.level) * this.equipData.hpGrow);
                    break;
                case AttributeQueue.MagicDefend:
                    prop = (float)(currentAttribute.GetProp(propId) + (level - this.level) * this.equipData.mdefGrow);
                    break;
                case AttributeQueue.PhyDefend:
                    prop = (float)(currentAttribute.GetProp(propId) + (level - this.level) * this.equipData.pdefGrow);
                    break;
            }

            return prop;
        }

        public List<AttributeQueue> GetStrengthenPropIds()
        {

            List<AttributeQueue> propIds = new List<AttributeQueue>();
            if (this.equipData.attGrow > 0)
            {
                propIds.Add(AttributeQueue.Attack);
            }
            if (this.equipData.mdefGrow > 0)
            {
                propIds.Add(AttributeQueue.MagicDefend);
            }
            if (this.equipData.hpGrow > 0)
            {
                propIds.Add(AttributeQueue.Hp);
            }
            if (this.equipData.pdefGrow > 0)
            {
                propIds.Add(AttributeQueue.PhyDefend);
            }

            return propIds;
        }

        public float GetEquipPropInRefineLevel(AttributeQueue propId, int refinelv)
        {
            float prop = 0;

            switch (propId)
            {
                case AttributeQueue.Attack:
                    prop = (float)(refinelv * this.equipRefineData.attGrow);
                    break;
                case AttributeQueue.Hp:
                    prop = (float)(refinelv * this.equipRefineData.hpGrow);
                    break;
                case AttributeQueue.MagicDefend:
                    prop = (float)(refinelv * this.equipRefineData.mdefGrow);
                    break;
                case AttributeQueue.PhyDefend:
                    prop = (float)(refinelv * this.equipRefineData.pdefGrow);
                    break;
                case AttributeQueue.cri:
                    prop = (float)(refinelv * this.equipRefineData.criGrow);
                    break;
                case AttributeQueue.dodge:
                    prop = (float)(refinelv * this.equipRefineData.perevadeGrow);
                    break;
                case AttributeQueue.hit:
                    prop = (float)(refinelv * this.equipRefineData.perhitGrow);
                    break;
                case AttributeQueue.antiCri:
                    prop = (float)(refinelv * this.equipRefineData.anticriGrow);
                    break;
            }

            return prop;
        }

        public List<AttributeQueue> GetRefinePropIds()
        {
            List<AttributeQueue> propIds = new List<AttributeQueue>();
            if (this.equipRefineData.attGrow > 0)
            {
                propIds.Add(AttributeQueue.Attack);
            }
            if (this.equipRefineData.mdefGrow > 0)
            {
                propIds.Add(AttributeQueue.MagicDefend);
            }
            if (this.equipRefineData.hpGrow > 0)
            {
                propIds.Add(AttributeQueue.Hp);
            }
            if (this.equipRefineData.pdefGrow > 0)
            {
                propIds.Add(AttributeQueue.PhyDefend);
            }
            if (this.equipRefineData.criGrow > 0)
            {
                propIds.Add(AttributeQueue.cri);
            }
            if (this.equipRefineData.perevadeGrow > 0)
            {
                propIds.Add(AttributeQueue.dodge);
            }
            if (this.equipRefineData.perhitGrow > 0)
            {
                propIds.Add(AttributeQueue.hit);
            }
            if (this.equipRefineData.anticriGrow > 0)
            {
                propIds.Add(AttributeQueue.antiCri);
            }

            return propIds;
        }

        /// <summary>
        /// 精炼属性
        /// </summary>
        public BaseAttributeData CurrentRefineAttribtue
        {
            get
            {
                return currentRefineAttribute;
            }
        }

        /// <summary>
        /// 装备品质名字“x色xx”
        /// </summary>
        /// <returns></returns>
        public string GetQualityWord()
        {
            string str = "";
            str += TableTextBlo.GetText("quality_color_" + equipData.quality.ToString()).Replace("\\r", "\r").Replace("\\n", "\n");
            str += TableTextBlo.GetText("equip_type_" + equipData.type.ToString()).Replace("\\r", "\r").Replace("\\n", "\n");

            return str;
        }

        public string GetWeaponNameWithHero(string heroid)
        {
            List<TableWeaponRes> weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroid, equipId);

            if (weapons.Count > 0 && weapons[0].nameRes != "")
            {
                return TableTextBlo.GetText("equip_name_" + weapons[0].nameRes);
            }

            return TableTextBlo.GetText("name_" + equipId);
        }

        public string GetWeaponDesc()
        {
            List<TableWeaponRes> weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(lastHeroId, equipId);

            if (weapons.Count > 0 && weapons[0].nameRes != "")
            {
                return TableTextBlo.GetText("desc_equip_" + weapons[0].nameRes);
            }

            return TableTextBlo.GetText("desc_" + equipId);
        }

        #endregion
    }
}
